package service.impl;

import dbconnection.ConnectionFactory;
import service.Product;

import java.sql.*;
import java.util.ArrayList;

import common.Split;

/**
 * Created by Administrator on 2016/12/15.
 */
public class ProductImpl implements Product{

    public static int getUserId(String username) throws Exception {
        ConnectionFactory connectionFactory=new ConnectionFactory();
        int userId=0;
        Connection connection=connectionFactory.getConnection();
        String sql="select userId from jc_user where userName=?";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet=preparedStatement.executeQuery();
        while (resultSet.next()){
            userId=resultSet.getInt("userId");
        }
        connection.close();
        return userId;//返回userId
    }

    public String productInit() throws Exception {
        ConnectionFactory connectionFactory=new ConnectionFactory();
        Connection connection=connectionFactory.getConnection();
        ArrayList<String> arrayList=new ArrayList<String>();
        String sql="select productName,price from jc_product";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            arrayList.add(resultSet.getString("productName"));
            arrayList.add(resultSet.getInt("price")+"");
        }
        String init="";
        for(int i=0;i<arrayList.size();i++){
            init+=arrayList.get(i);
            if(i!=arrayList.size()-1)
                init+=",";
        }
//        System.out.println("--------"+init);
        connection.close();
        return init;//爱马仕包,阿玛尼牛仔双肩包,阿迪达斯椰子,潘多拉戒指 ,草雉剑,手工品,衣帽服饰,电子配件,化妆品,烟酒,智能手机,PU皮包,自行车,盐,斯库水,生蚝,鳗鱼,贝壳
    }

    public String productHav(int userId) throws Exception {
        ConnectionFactory connectionFactory=new ConnectionFactory();
        Connection connection=connectionFactory.getConnection();
        ArrayList<String> nameList=new ArrayList<>();
        ArrayList<String> priceList=new ArrayList<>();
        String sql="select productName from jc_product";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            nameList.add(resultSet.getString("productName"));
        }
        sql="select productPrice from jc_userproperty where userId=?";
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setInt(1,userId);
        resultSet=preparedStatement.executeQuery();
        String productPri="";
        while(resultSet.next()){
            productPri=resultSet.getString("productPrice");
        }
        Split split=new Split();
        priceList=split.splitList(productPri);
        String have="";
        for(int i=0;i<nameList.size();i++){
            have+=nameList.get(i)+","+priceList.get(i);
            if(i!=nameList.size()-1)
                have+=",";
        }
        connection.close();
        return have;
    }
}
